package com.qianfeng.zhangwei.sybl.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.qianfeng.zhangwei.sybl.BaseApp;
import com.qianfeng.zhangwei.sybl.R;
import com.qianfeng.zhangwei.sybl.bean.Gift;
import com.qianfeng.zhangwei.sybl.utils.ImageLoaderHelper;

import java.util.List;

/**
 * @说 明：
 * @项目名称：Sz1504
 * @包 名： com.qianfeng.zhangwei.sybl.adapter
 * @类 名： GameInfoAdapter
 * @创 建人： zhangwei
 * @创建时间：2015-07-29 09:45
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注：
 */
public class GameInfoAdapter extends AppBaseAdapter<Gift> {
    /**
     * @param list
     * @param context
     */


    public GameInfoAdapter(List<Gift> list, Context context) {
        super(list, context);
        options = ImageLoaderHelper.getInstance().getDisplayImageOptions(R.mipmap.ic_android, R.mipmap.ic_android);
    }

    @Override
    public View getItemView(int position, View convertView, ViewGroup parent) {
        Gift gift = list.get(position);

        ViewHolder vh;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_gift_content, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.nanmeTv.setText(gift.getName());
        vh.contentTv.setText(gift.getContent());
        if (!TextUtils.isEmpty(gift.getConsume()) && !gift.getConsume().equals("0.0")) {
            vh.consumeBtn.setText(gift.getConsume() + "Q币");
        } else {
            vh.consumeBtn.setText("免费领取");
        }
        vh.remainTv.setText(gift.getRemain());
        vh.consumeBtn.setOnClickListener(new MyOnClickListener(position));

        ImageLoader.getInstance().displayImage(list.get(position).getIcon(), vh.img, options);
        return convertView;
    }

    /**
     * button  checkbox raditobutoon  EditText  等等  优先获取焦点
     */
    private static class ViewHolder {
        private ImageView img;
        private TextView nanmeTv;
        private TextView contentTv;

        private Button consumeBtn;
        private TextView remainTv;//gift_content_remain_txt2

        public ViewHolder(View convertView) {
            img = (ImageView) convertView.findViewById(R.id.gift_content_icon_img);
            nanmeTv = (TextView) convertView.findViewById(R.id.gift_content_name_txt);
            contentTv = (TextView) convertView.findViewById(R.id.gift_content_content_txt);
            consumeBtn = (Button) convertView.findViewById(R.id.gift_content_consume_btn);
            remainTv = (TextView) convertView.findViewById(R.id.gift_content_remain_txt2);
        }
    }

    private class MyOnClickListener implements View.OnClickListener {
        private int position;

        public MyOnClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            BaseApp.getInstance().showToast(list.get(position).getIcon());
        }
    }


}
